package com.smallA.controller;

import com.smallA.constant.DataConstant;
import com.smallA.po.Exam;
import com.smallA.po.Grade;
import com.smallA.po.TopicType;
import com.smallA.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author leon
 * @create 2021/8/14 12:24
 */
@Controller
public class TeacherController {
    private UserService userService;
    private GradeService gradeService;
    private ExamService examService;
    private ExamQuestionService examQuestionService;
    private TestService testService;

    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }

    private QuestionService questionService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setTestService(TestService testService) {
        this.testService = testService;
    }

    public void setExamService(ExamService examService) {
        this.examService = examService;
    }


    public void setExamQuestionService(ExamQuestionService examQuestionService) {
        this.examQuestionService = examQuestionService;
    }


    public void setGradeService(GradeService gradeService) {
        this.gradeService = gradeService;
    }


    @RequestMapping("/Teacher/exam")
    @ResponseBody
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Object Exam(int id) {
        Map<String, Object> map = examQuestionService.selectExamById(id);
        Exam exam = new Exam();
        exam.setQuestion(map);
//        model.addAttribute(DataConstant.EXAM_DETAIL, exam);
//        return "teacher/exam";
        return exam;
    }


    /**
     * 获取班级列表
     * @param
     * @return
     */
    @RequestMapping("/Teacher/class")
//    @Secured(AccessConstant.TEACHER_ACCESS)
    @ResponseBody
    public Object classroom(int tid){
//    public Object classroom(HttpServletRequest request){
//        Client user = (Client) request.getSession().getAttribute(DataConstant.STUDENT_USER);
//        model.addAttribute(DataConstant.CLASS_LIST,userService.selectClassById(user.getId()));
//        return "teacher/classInfo";
        return userService.selectClassById(tid);
    }

    /**
     * 班级考试情况
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/Teacher/classTest")
    @ResponseBody
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Object classTest(int id,Model model){
        Map<String,Object> map = new HashMap<>();
        map.put(DataConstant.ALL_TEST,userService.selectTestByClassId(id));
        map.put(DataConstant.CLASSROOM,userService.selectClassByClassId(id));
        return map;
//        model.addAttribute(DataConstant.ALL_TEST,userService.selectTestByClassId(id));
//        model.addAttribute(DataConstant.CLASSROOM,userService.selectClassByClassId(id));
//        return "teacher/testInfo";
    }

    /**
     * 班级考试成绩情况
     * @param tid
     * @param cid
     * @return
     */
    @ResponseBody
    @RequestMapping("/Teacher/classGrade")
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Object classGrade(int tid, int cid){
//
//        model.addAttribute(DataConstant.GRADE_LIST,gradeService.selectGradeByTestId(tid));
//        model.addAttribute(DataConstant.STUDENT_LIST,userService.selectStuByClassId(cid));
//        return "teacher/classGrade";
        Map<String,Object> map = new HashMap<>();
        map.put(DataConstant.GRADE_LIST,gradeService.selectGradeByTestId(tid));
        map.put(DataConstant.STUDENT_LIST,userService.selectStuByClassId(cid));
        return map;
    }

    @ResponseBody
    @RequestMapping("/Teacher/getExam")
//    @Secured(AccessConstant.TEACHER_ACCESS)
    public Object getExam(int gid) {
        Exam exam = examService.selectExamById(gid);
//        model.addAttribute(DataConstant.EXAM_DETAIL, exam);
        return exam;
    }

    @ResponseBody
    @RequestMapping("/Teacher/autoExam")
    public Object autoTest() {
        List<TopicType> topicTypes = testService.selectAllTopicTpe();
        Map<String,Object> map = new HashMap<>();
        map.put(DataConstant.TOPIC_TYPE_DATA, topicTypes);
        map.put("Question",examQuestionService.selectAllQuestion());
        return map;
//        model.addAttribute(DataConstant.TOPIC_TYPE_DATA, topicTypes);
//        model.addAttribute("Question",examQuestionService.selectAllQuestion());
//        return "teacher/autoExam";
    }

    @ResponseBody
    @RequestMapping("/Teacher/autoAdd")
    public Object autoAdd(Integer[] topicTypeIds, Integer choiceCount, Integer fillCount, Integer shortCount, String analyze, int score,int tid) {
        Exam exam = examService.autoAdd(topicTypeIds,choiceCount, fillCount,shortCount,analyze,score,tid);
//        model.addAttribute(DataConstant.EXAM_DETAIL,exam);
//        return "teacher/exam";
        return exam;
    }

//    @Secured(AccessConstant.TEACHER_ACCESS)
@RequestMapping("/Teacher/submit")
    public String submit(Model model){
        String time = "10";
        model.addAttribute("time",time);
        return "teacher/submit";
    }

    @ResponseBody
    @RequestMapping("/Teacher/endTime")
    public Long getEndTime(Integer gid){
        Grade grade = gradeService.selectGradeById(gid);
        long i = grade.getTest().getTestTime()* 60000L+ grade.getTest().getBeginDate().getTime()-System.currentTimeMillis();
        System.out.println(i);
        return i;
    }

    @ResponseBody
    @RequestMapping("/Teacher/uploadFile")
    public Object upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        String path = request.getServletContext().getRealPath("/upload");
        return questionService.insertQuestion(path, file);

//        request.setAttribute(DataConstant.MSG, questionService.insertQuestion(path, file));
//        return "student/other";

    }
}
